//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    fbstatus: 'none',
    izclist: [],
    ad1: [],
    ad2: [],
    banner: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    indicatorColor: 'rgba(0, 0, 0, .6)',
    IsShowCountDown: 0,
    IsShowPic: 0,
    PicUrl: '',
    TipH5Url: '',
    dingshi: 5,
    dingshiDs: 5,

    menuTop: '',
    navbarInitTop: 0,
    isFixed: false,
    changeTuijianIndex: 0,
    tuijianBiaoqian: ['资产处置', '招标公告'],
    tuijianBiaoqianIndex: 0,
    intCurrPage: 1,
    headText: '',
    city: wx.getStorageSync('isCity'),
    strProName: '',
    cellUrl: "",
    showInlet: false,
    swiperLists: [{
      name: '推荐',
      List: [],
      tabIndex1: 0,
      tabIndex2: 0,
      isMore: true,
      intCurrPage: 1,
      tab: ['资产处置', '招标公告']
    }, {
      name: '附近商机',
      List: [],
      tabIndex1: 0,
      tabIndex2: 0,
      isMore: true,
      intCurrPage: 1,
      tab: ['资产处置', '招标公告']
    },],

  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  towoyaochuzhi: function (e) {

  },
  togongqiuxinxi: function () {
    wx.navigateTo({
      url: '../gongqiuxinxi/index'
    })
  },

  toCompany: function (e) {

    if (e.currentTarget.dataset.uid != 0) {
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/shangpu?longUID=' + e.currentTarget.dataset.uid
      })
    } else {
      var url = e.currentTarget.dataset.url;
      console.log(url)
      if (url.substring(0, 20) == 'https://m.feijiu.net' || url.substring(0, 19) == 'http://m.feijiu.net' || url.substring(0, 19) == 'https://s.jupai.net') {
        // wx.navigateTo({
        //   url: '../other/web?url=' + url
        // })
        wx.navigateTo({
          url: '/pages/other/web',
          success: function (res) {
            // 通过 eventChannel 向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', {
              url: url
            })
          }
        })
      } else {
        wx.showToast({
          title: '此功能仅限app使用',
          icon: 'none'
        })
      }
    }
  },
  toSearch: function () {
    wx.navigateTo({
      url: '/pages/search/index'
    })
  },
  zichanchuzhi: function () {
    wx.navigateTo({
      url: '../zichanchuzhi/index'
    })
  },
  zhaobiaogonggao: function (e) {
    wx.navigateTo({
      url: '/pages/zhaobiaogonggao/detailed?uuid=' + e.currentTarget.dataset.uuid,
    })
  },
  news: function () {
    wx.navigateTo({
      url: '../news/index'
    })
  },
  baojiayuce: function () {
    wx.navigateTo({
      url: '/packageA/pages/index/index'
    })
  },
  rengongfabu: function () {
    var that = this
    wx.showModal({
      title: '拨打电话发布信息',
      content: '0311-87106666',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (res) => {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '0311-87106666'
          })
        }
      }
    })
  },
  zichanfabu: function () {
    if (app.checkFjLogin()) {
      wx.navigateTo({
        url: '../create/zichan'
      })
    } else {
      wx.navigateTo({
        url: '../../pages/login/index'
      });
    }
  },
  gongqiufabu: function (e) {
    let url = e.currentTarget.dataset.url,
      classid = e.currentTarget.dataset.shid
    // let classid = this.data.classId
    // if(classid == 2){
    //   classid =3
    // }else if(classid == 3){
    //   classid =5
    // }else if(classid == 4){
    //   classid = 10
    // }
    // if (app.checkFjLogin()) {
    if (url == 25002) { //供应
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/index?GQ_classid=' + classid + '&pageName=' + this.data.headText + '&shid=1'
        // url: '../../../pages/create/gongqiu?shid=' + e.currentTarget.dataset.shid
      })
    } else if (url == 25003) { //求购
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/index?GQ_classid=' + classid + '&pageName=' + this.data.headText + '&shid=2'
        // url: '../../../pages/create/gongqiu?shid=' + e.currentTarget.dataset.shid
      })
    } else if (url == 25004) { //资产处置
      wx.navigateTo({
        url: '/pages/zichanchuzhi/index?GQ_classid=' + classid + '&pageName=' + this.data.headText
        // url: '../../../pages/create/gongqiu?shid=' + e.currentTarget.dataset.shid
      })
    } else if (url == 25005) { //招标拍卖
      wx.reLaunch({
        url: '/pages/zhaobiaogonggao/index?GQ_classid=' + classid + '&pageName=' + this.data.headText,
      })
    } else if (url == 25006) { //报价预测
      if (classid == '4.') {
        wx.navigateTo({
          url: '/packageA/pages/index/baojiaNavDetal/baojiaNavDetail?ClassIndex=' + classid,
        })
      } else {
        wx.navigateTo({
          url: '/packageA/pages/index/index',
        })
      }
    }

    // } else {
    //   wx.navigateTo({
    //     url: '../../../pages/login/index'
    //   });
    // }
  },
  completemessage: function completemessage(res) {
    console.log(res, "res");
  },
  clickCell: function () {
    const url = 'api/FeijiuApp/Feijiu_Community_ClickADD'
    const LoginUID = wx.getStorageSync('FjLoginLog').UID ? wx.getStorageSync('FjLoginLog').UID : "0"
    const _this = this
    const click_platform = "4"
    const systemInfo = wx.getSystemInfoSync();
    const click_system = systemInfo.platform === 'ios' ? "2" : "1"
    app.getDaoPostN(url, { LoginUID, click_position: _this.data.classId, click_platform, click_system }, function (res) {

    })
  },
  startmessage: function startmessage(res) {
    // const url = 'api/FeijiuApp/Feijiu_Community_ClickADD'
    // const LoginUID = wx.getStorageSync('FjLoginLog').UID
    // const _this=this
    // const click_platform="4"
    // const systemInfo = wx.getSystemInfoSync();
    // const click_system=systemInfo.platform === 'ios'?"2":"1"
    // app.getDaoPostN(url, {LoginUID,click_position:_this.data.classId,click_platform,click_system},function(res){

    // })
  },
  closeInlet() {
    this.setData({
      showInlet: false
    })
    const url = 'api/FeijiuApp/Feijiu_Community_ClosePopUp'
    const LoginUID = wx.getStorageSync('FjLoginLog').UID
    const _this = this
    const systemInfo = wx.getSystemInfoSync();
    const popup_system = systemInfo.platform === 'ios' ? "2" : "1"
    app.getDaoPostN(url, { LoginUID, popup_position: _this.data.classId, popup_platform: "4", popup_system }, function (res) {

    })
  },
  getDetail(popup_position) {
    const LoginUID = wx.getStorageSync('FjLoginLog').UID
    if (!LoginUID) {
      this.setData({
        showInlet: true
      })
      return
    }
    const url = 'api/FeijiuApp/Feijiu_Community_IsPopUp'
    const _this = this
    const popup_platform = "4"
    app.getDaoPostN(url, { LoginUID, popup_position, popup_platform }, function (r) {
      if (r.code == 200) {
        _this.setData({
          showInlet: r.data == 1
        })
      }
    })
  },
  onLoad: function (option) {
    this.getDetail(option.classId)
    var that = this,
      ag = app.globalData
    //定义导航
    that.setData({
      headText: option.name,
      classId: option.classId,
      cellUrl: option.classId == 1 ? 'https://work.weixin.qq.com/gm/de87c12fdfded43877996a4f08091623' : 'https://work.weixin.qq.com/gm/e5c7339ff0b91da4ca0f8607017a2c24',
      navHeight: ag.navHeight, //导航栏高度
      navTop: ag.navTop, //导航栏距顶部距离
      navObj: ag.navObj, //胶囊的高度
      navObjWid: ag.navObjWid, //胶囊宽度+距右距离
      tabbarHeight: ag.tabbarHeight, //tabbar高度
      windowHeight: ag.windowHeight, //窗口高度
      screenHeight: ag.screenHeight, // 屏幕高度
      statusBarHeight: ag.statusBarHeight, //状态栏
      // stickylistHeight: ag.screenHeight - ag.statusBarHeight - ag.navHeight
      stickylistHeight: ag.screenHeight - ag.navHeight - 38 - 35 + 10,
      // stickylistHeight: ag.windowHeight - ag.navHeight - 38 - 35 + 10 + ag.tabbarHeight
    }, async () => {
      wx.showLoading({
        title: '数据加载中',
      })
      await that.queryIndex() //首页
      await this.queryIndexList(0, 0) //推荐商机
      await this.queryIndexList(1, 0) //附近商机
    })

  },
  preventTouchMove: function () {
    // body...
  },
  //关闭发布菜单
  shutdownfb: function () {
    this.setData({
      fbstatus: 'none'
    })

  },
  //点开发布菜单
  openfb: function () {
    this.setData({
      fbstatus: 'block'
    })
  },

  onShareAppMessage: function (options) {
    var that = this;
    var shareObj = {
      title: '',
      path: '/pages/index/index',
      imgUrl: '../../images/zhuanf.png',
    }
    return shareObj;
  },
  sub: function (str, n) {
    var r = /[^\x00-\xff]/g;
    if (str.replace(r, "mm").length <= n) {
      return str;
    }
    var m = Math.floor(n / 2);
    for (var i = m; i < str.length; i++) {
      if (str.substr(0, i).replace(r, "mm").length >= n) {
        return str.substr(0, i) + "...";
      }
    }
    return str;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    // let a = this.data.swiperLists
    // a[0].List = [];
    // a[0].intCurrPage = 0,
    //   a[0].isMore = true
    // a[1].List = []
    // a[1].intCurrPage = 0,
    //   a[1].isMore = true
    // this.setData({
    //   swiperLists: a
    // }, async () => {
    //   await this.queryIndexList(0, a[0].tabIndex2) //推荐商机
    //   await this.queryIndexList(1, a[1].tabIndex2) //附近商机
    // })
    // this.queryIndexList()
    // await this.queryIndexList(0, 0) //推荐商机
    // await this.queryIndexList(1, 0) //附近商机
  },
  onReady() {
    //获取当前组件到顶部的高度
    if (this.data.navbarInitTop == 0) {
      // wx.createSelectorQuery().select('#navbar').boundingClientRect((rect) => {
      //   console.log(rect, 'rect')
      //   if (rect && rect.top > 0) {
      //     var navbarInitTop = rect.top;
      //     this.data.navbarInitTop = navbarInitTop
      //   }
      // }).exec();
    }
  },
  c_pop: function () {
    this.setData({
      IsShowPic: 0
    })
  },
  /**
   * 监听滚动事件
   */
  onPageScroll: function (e) {
    // console.log('scroll', e, this.data.navbarInitTop)
    var scrollTop = e.scrollTop;
    var isSatisfy = scrollTop >= (this.data.navbarInitTop - this.data.navHeight);
    if (this.data.isFixed == isSatisfy) {
      return false;
    } else {
      this.setData({
        isFixed: isSatisfy,
        liststyle: `top:${this.data.navHeight}px`,
        listTowstyle: `top:${this.data.navHeight + this.data.tuijianTitleHeight}px`,
        liststylestick: `margin-top:168rpx;`, //168
        // liststylestick: `top:${(this.data.navHeight)*2}rpx;position: relative;`,
        a: `position: relative;z-index: 9;top:38px`
      });
    }
  },
  // 改版 查询二级页面
  queryIndex() {
    let input = {
      ClassIndex: this.data.classId,
    },
      that = this
    if (wx.getStorageSync('FjLoginLog')) {
      input.LoginUID = wx.getStorageSync('FjLoginLog').UID
    }
    app.getDaoPost('api/FeijiuApp/Feijiu_Get_ClassIndex', input, r => {
      console.log(r, '二级页面')
      if (r.code == 200) {
        that.setData({
          ADList: r.data.ADList,
          Banner: r.data.Banner,
          ListIcon: r.data.ListIcon,

        }, () => {
          if (that.data.navbarInitTop == 0) {
            const query = wx.createSelectorQuery()
            query.select('#navbar').boundingClientRect((rect) => {
              if (rect && rect.top > 0) {
                var navbarInitTop = rect.top;
                that.data.navbarInitTop = navbarInitTop
              }
              let height = rect.height;
              console.log("height: ", height);
              that.data.tuijianTitleHeight = height
            }).exec();
            // query.select(".layer12")
            //   .boundingClientRect((rect) => {
            //     let height = rect.height;
            //     console.log("height: ", height);
            //     that.data.tuijianTitleHeight = height
            //   })
            //   .exec();
          }
        })
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        })
      }
    })
  },
  //商机切换
  changeTuijian(e) {
    if (this.data.city == '') {
      this.setData({
        isLoction: true
      })
    }
    this.setData({
      changeTuijianIndex: e.currentTarget.dataset.index,
      swiperItemCurrent: e.currentTarget.dataset.index,
    }, async () => {
      // await this.queryIndexList()
    })
  },
  // 资产 公告 模块切换
  cilckSahngjiTuijian(e) {
    let a = this.data.swiperLists,
      indexb = this.data.changeTuijianIndex
    a[indexb].List = [],
      a[indexb].tabIndex2 = e.currentTarget.dataset.index,
      a[indexb].isMore = true,
      a[indexb].intCurrPage = 1

    this.setData({
      // tuijianBiaoqianIndex: e.currentTarget.dataset.index,
      swiperLists: a,
    }, async () => {
      await this.queryIndexList(this.data.changeTuijianIndex, e.currentTarget.dataset.index)
    })
  },
  swiperListChange(e) { //列表左右滑动
    this.setData({
      changeTuijianIndex: e.detail.current, //改变推荐选中状态
      swiperItemCurrent: e.detail.current, //改变轮播图current
    })
  },
  goback() {
    wx.navigateBack({
      delta: 0,
    })
  },
  // 查询首页推荐：资产 公告 供应/求购
  queryIndexList(tabIndex = this.data.changeTuijianIndex, tab2Index = this.data.swiperLists[this.data.changeTuijianInde].tabIndex2) {
    wx.showLoading({})
    let that = this,
      Indexlist = that.data.swiperLists,
      changeTuijianIndex = tabIndex, //推荐0 附近1
      tuijianBiaoqianIndex = tab2Index, //资产 公告 供求
      postparam = '',
      ClassIndex = that.data.classId,
      LoginUID = wx.getStorageSync('FjLoginLog').UID,
      strCity = that.data.city, //城市
      strProName = that.data.strProName, //省
      intCurrPage = Indexlist[changeTuijianIndex].intCurrPage, //that.data.intCurrPage,
      strKey = '',
      strClass = '',
      inupt = { //参数

      }
    if (changeTuijianIndex == 0) { //推荐0
      if (tuijianBiaoqianIndex == 0) {
        postparam = 'Feijiu_Get_ClassIndex_TjZCList'
        inupt = {
          LoginUID,
          ClassIndex,
          strProName,
          strCity,
          intCurrPage,

        }
      } else if (tuijianBiaoqianIndex == 1) {
        postparam = 'Feijiu_Get_ClassIndex_TjGGList'
        inupt = {
          LoginUID,
          ClassIndex,
          strProName,
          strCity,
          intCurrPage,

        }
      }
    } else if (changeTuijianIndex == 1) {
      if (tuijianBiaoqianIndex == 0) {
        postparam = 'Feijiu_Get_ClassIndex_NewZCList'
        inupt = {
          ClassIndex,
          strClass,
          strKey,
          intCurrPage,
        }
      } else if (tuijianBiaoqianIndex == 1) {
        postparam = 'Feijiu_Get_ClassIndex_NewGGList'
        inupt = {
          ClassIndex,
          strClass,
          strKey,
          intCurrPage,
        }
      }
    }

    app.getDaoPost(`api/FeijiuApp/${postparam}`, inupt, r => {
      wx.hideLoading({})
      console.log(r, '推荐')
      if (r.code == 200) {
        if (r.data.data.length >= 15) {
          Indexlist[changeTuijianIndex].List = [...Indexlist[changeTuijianIndex].List, ...r.data.data];
          Indexlist[changeTuijianIndex].intCurrPage = Indexlist[changeTuijianIndex].intCurrPage + 1;
          Indexlist[changeTuijianIndex].isMore = true;
          that.setData({
            swiperLists: Indexlist,
          }, () => {
            console.log('赋值完了')
            wx.hideLoading({})
          })

        } else if (r.data.data.length < 15) {
          Indexlist[changeTuijianIndex].List = [...Indexlist[changeTuijianIndex].List, ...r.data.data];
          // Indexlist[changeTuijianIndex].intCurrPage =  Indexlist[changeTuijianIndex].intCurrPage;
          Indexlist[changeTuijianIndex].isMore = false;
          that.setData({
            swiperLists: Indexlist,
          }, () => {
            wx.hideLoading({})
          })
        } else {
          Indexlist[changeTuijianIndex].List = [...Indexlist[changeTuijianIndex].List, ...r.data.data];
          // Indexlist[changeTuijianIndex].intCurrPage =  Indexlist[changeTuijianIndex].intCurrPage+1;
          Indexlist[changeTuijianIndex].isMore = false;
          that.setData({
            swiperLists: Indexlist,
          }, () => {
            wx.hideLoading({})
          })
        }
      } else {
        wx.hideLoading({})
        wx.showToast({
          title: r.message,
        })
      }
    })

  },
  //scroll 触底加载更多
  indexListMore(e) {
    let changeTuijianIndex = this.data.changeTuijianIndex,
      swiperLists = this.data.swiperLists
    if (swiperLists[changeTuijianIndex].isMore == true) {
      this.queryIndexList(changeTuijianIndex, swiperLists[changeTuijianIndex].tabIndex2)
    } else {
      wx.showToast({
        title: '暂时没有更多拉',
        icon: 'none'
      })
    }
  },
  // 调起电话
  callPhone(e) {
    let ph = e.currentTarget.dataset.ph
    wx.makePhoneCall({
      phoneNumber: ph //仅为示例，并非真实的电话号码
    })
  },
  //资产处置详情跳转
  zichanchuzhiDetail(e) {
    // console.log(e, '===')
    let uuid = e.currentTarget.dataset.uuid
    wx.navigateTo({
      url: '/packageA/pages/zichanchuzhi/detailed?gqid=' + uuid,
    })
  },
})